package simbot.example.demo.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * Created by henery on 2017/6/13.
 */
@Component
public class JsonArraySerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String str, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeStartArray();
        if (!StringUtils.isEmpty(str)) {
            String[] array = str.split(",");
            for (int i = 0; i < array.length; i++) {
                jsonGenerator.writeString(array[i]);
            }
        }

        jsonGenerator.writeEndArray();
    }
}
